﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using ESoft.Core.DataAccess.DAO;
using ESoft.Core.DataAccess.Entity;
using ESoft.Core.Web.VO;
using Microsoft.EntityFrameworkCore;

namespace ESoft.Core.DataAccess.Service.Impl
{
    public class BaseService : IBaseService
    {
        private readonly IBaseDao _baseDao;

        public BaseService(IBaseDao dao)
        {
            _baseDao = dao;
        }


        public Task<T> FirstOrDefaultAsync<T>(Expression<Func<T, bool>> predicate) where T : BaseModel, new()
        {
            return _baseDao.FirstOrDefaultAsync(predicate);
        }

        public T Add<T>(T entity) where T : BaseModel, new()
        {
            return _baseDao.Add(entity);
        }

        public Task<T> AddAsync<T>(T entity) where T : BaseModel, new()
        {
            return _baseDao.AddAsync<T>(entity);
        }

        public int Count<T>(Expression<Func<T, bool>> predicate) where T : BaseModel, new()
        {
            return _baseDao.Count(predicate);
        }

        public Task<int> CountAsync<T>(Expression<Func<T, bool>> predicate) where T : BaseModel, new()
        {
            return _baseDao.CountAsync<T>(predicate);
        }

        public bool Delete<T>(IEnumerable<T> entities) where T : BaseModel, new()
        {
            return _baseDao.Delete(entities);
        }

        public bool Delete<T>(T entity) where T : BaseModel, new()
        {
            return _baseDao.Delete(entity);
        }

        public Task<T> UpdateAsync<T>(T entity) where T : BaseModel, new()
        {
            return _baseDao.UpdateAsync<T>(entity);
        }

        public bool Delete<T>(object id) where T : BaseModel, new()
        {
            return _baseDao.Delete<T>(id);
        }

        public bool Exist<T>(Expression<Func<T, bool>> predicate) where T : BaseModel, new()
        {
            return _baseDao.Exist<T>(predicate);
        }

        public T Find<T>(object id) where T : BaseModel, new()
        {
            return _baseDao.Find<T>(id);
        }

        public Task<T> FindAsync<T>(object id) where T : BaseModel, new()
        {
            return _baseDao.FindAsync<T>(id);
        }

        public T First<T>(Expression<Func<T, bool>> predicate) where T : BaseModel, new()
        {
            return _baseDao.First(predicate);
        }

        public Task<int> SumAsync<T>(Expression<Func<T, bool>> predicate, Expression<Func<T, int>> selector) where T : BaseModel, new()
        {
            return _baseDao.SumAsync<T>(predicate, selector);
        }

        public List<T> QueryEntities<T>() where T : BaseModel, new()
        {
            return _baseDao.LoadEntities<T>().ToList();
        }

        public Task<List<T>> LoadEntitiesAsync<T>() where T : BaseModel, new()
        {
            return _baseDao.LoadEntitiesAsync<T>();
        }

        public List<T> QueryEntities<T>(Expression<Func<T, bool>> predicate) where T : BaseModel, new()
        {
            return _baseDao.LoadEntities(predicate).ToList();
        }

        public Task<List<T>> QueryEntitiesAsync<T>(Expression<Func<T, bool>> predicate) where T : BaseModel, new()
        {
            return _baseDao.LoadEntitiesAsync<T>(predicate);
        }

        public decimal Sum<T>(Expression<Func<T, bool>> predicate, Expression<Func<T, decimal>> selector) where T : BaseModel, new()
        {
            return _baseDao.Sum(predicate, selector);
        }

        public int Sum<T>(Expression<Func<T, bool>> predicate, Expression<Func<T, int>> selector) where T : BaseModel, new()
        {
            return _baseDao.Sum(predicate, selector);
        }

        public bool Update<T>(T entity) where T : BaseModel, new()
        {
            return _baseDao.Update(entity);
        }

        public List<T> QueryPageEntities<T>(PageInput page, Expression<Func<T, bool>> predicate, SortVo sort)
            where T : BaseModel, new()
        {
            return this._baseDao.LoadPageEntities(page, predicate, sort).ToList();
        }

        public async Task<List<T>> QueryPageEntitiesAsync<T>(PageInput page, Expression<Func<T, bool>> predicate, SortVo sort) where T : BaseModel, new()
        {
            return await _baseDao.LoadPageEntities(page, predicate, sort).ToListAsync();
        }
    }
}
